﻿package com.designoutput.ui.controls
{
    import com.designoutput.event.ChangeEvent;
    import com.designoutput.icon.CheckboxCheckedHover;
    import com.designoutput.icon.CheckboxUncheckedHover;
    
    import flash.display.*;
    import flash.events.*;

    public class Checkbox extends Sprite
    {
        private var _checked:Boolean = false;
        internal var title:Literal;
        internal var uc:IconButton;
        internal var ck:IconButton;

        public function Checkbox(label:String = "")
        {
            this.uc = new IconButton(new CheckboxUncheckedHover(0, 0));
            this.ck = new IconButton(new CheckboxCheckedHover(0, 0));
            this.ck.y = 1;
            this.uc.y = 3;
            this.ck.visible = false;
            addChild(this.uc);
            addChild(this.ck);
            if (label != "")
            {
                this.title = new Literal(label);
                this.title.x = 13;
                addChild(this.title);
            }
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.MouseDown);
        }

        private function MouseDown(evt:MouseEvent) : void
        {
            this._checked = Boolean(!this._checked);
            this.uc.visible = !this._checked;
            this.ck.visible = this._checked;
			evt.stopPropagation();
            dispatchEvent(new ChangeEvent(this._checked));
        }
		
		public function get checked():Boolean {
			return _checked;
		}

        public function check(param1:Boolean = true) : void
        {
            this._checked = param1;
            this.uc.visible = !this._checked;
            this.ck.visible = this._checked;
        }

    }
}
